package com.ruoyi.task.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.task.domain.CameraInfo;
import com.ruoyi.task.domain.ChannelConfig;
import com.ruoyi.task.domain.EquipmentConfig;
import com.ruoyi.task.domain.SceneCameraParamsInfo;
import com.ruoyi.task.domain.SceneParamsConfig;
import com.ruoyi.task.domain.SceneParamsInfo;
import com.ruoyi.task.mapper.CameraInfoMapper;
import com.ruoyi.task.mapper.ChannelConfigMapper;
import com.ruoyi.task.mapper.EquipmentConfigMapper;
import com.ruoyi.task.mapper.SceneCameraParamsMapper;
import com.ruoyi.task.mapper.SceneParamsInfoMapper;
import com.ruoyi.task.service.ISceneParamsInfoService;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 场景参数管理Service业务层处理
 *
 * @author admin
 * @date 2025-08-20
 */
@Service
@Slf4j
public class SceneParamsInfoServiceImpl implements ISceneParamsInfoService
{
    @Autowired
    private SceneParamsInfoMapper sceneParamsInfoMapper;

    @Autowired
    private SceneCameraParamsMapper sceneCameraParamsMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private CameraInfoMapper cameraInfoMapper;

    @Autowired
    private ChannelConfigMapper channelConfigMapper;

    @Autowired
    private EquipmentConfigMapper equipmentConfigMapper;

    /**
     * 查询场景参数管理
     *
     * @param id 场景参数管理主键
     * @return 场景参数管理
     */
    @Override
    public SceneParamsInfo selectSceneParamsInfoById(Long id)
    {
        return sceneParamsInfoMapper.selectSceneParamsInfoById(id);
    }

    /**
     * 查询场景参数管理列表
     *
     * @param sceneParamsInfo 场景参数管理
     * @return 场景参数管理
     */
    @Override
    public List<SceneParamsInfo> selectSceneParamsInfoList(SceneParamsInfo sceneParamsInfo)
    {
        if (StringUtils.isNotBlank(sceneParamsInfo.getScenariosType())) {
            sceneParamsInfo.setScenariosTypeList(
                    StringUtils.str2List(sceneParamsInfo.getScenariosType(), ",", true, true));
            sceneParamsInfo.setScenariosType(null);
        }

        return sceneParamsInfoMapper.selectSceneParamsInfoList(sceneParamsInfo);
    }

    /**
     * 新增场景参数管理
     *
     * @param sceneParamsInfo 场景参数管理
     * @return 结果
     */
    @Override
    public int insertSceneParamsInfo(SceneParamsInfo sceneParamsInfo)
    {
        // 判断是否已有相同的场景存在
        SceneParamsInfo dbSceneParamsInfo = sceneParamsInfoMapper.selectSceneParamsInfo(
                sceneParamsInfo.getScenariosType());
        if (dbSceneParamsInfo != null) {
            throw new BaseException("场景已存在");
        }
        // 查询场景名称
        String sceneName = sysDictDataService.selectDictLabel("task_scenarios_type",
                sceneParamsInfo.getScenariosType());
        sceneParamsInfo.setScenariosName(sceneName);
        sceneParamsInfo.setCreateTime(DateUtils.getNowDate());
        return sceneParamsInfoMapper.insertSceneParamsInfo(sceneParamsInfo);
    }

    /**
     * 修改场景参数管理
     *
     * @param sceneParamsInfo 场景参数管理
     * @return 结果
     */
    @Override
    public int updateSceneParamsInfo(SceneParamsInfo sceneParamsInfo)
    {
        sceneParamsInfo.setUpdateTime(DateUtils.getNowDate());
        return sceneParamsInfoMapper.updateSceneParamsInfo(sceneParamsInfo);
    }

    /**
     * 批量删除场景参数管理
     *
     * @param ids 需要删除的场景参数管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneParamsInfoByIds(Long[] ids)
    {
        return sceneParamsInfoMapper.deleteSceneParamsInfoByIds(ids);
    }

    /**
     * 删除场景参数管理信息
     *
     * @param id 场景参数管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneParamsInfoById(Long id)
    {
        return sceneParamsInfoMapper.deleteSceneParamsInfoById(id);
    }


    /**
     * 查询场景摄像头参数管理
     *
     * @param id 场景摄像头参数管理主键
     * @return 场景摄像头参数管理
     */
    @Override
    public SceneCameraParamsInfo selectSceneCameraParamsById(Long id)
    {
        return sceneCameraParamsMapper.selectSceneCameraParamsById(id);
    }

    /**
     * 查询场景摄像头参数管理列表
     *
     * @param sceneCameraParams 场景摄像头参数管理
     * @return 场景摄像头参数管理
     */
    @Override
    public List<SceneCameraParamsInfo> selectSceneCameraParamsList(
            SceneCameraParamsInfo sceneCameraParams) {
        return sceneCameraParamsMapper.selectSceneCameraParamsList(sceneCameraParams);
    }

    /**
     * 新增场景摄像头参数管理
     *
     * @param sceneCameraParams 场景摄像头参数管理
     * @return 结果
     */
    @Override
    public int insertSceneCameraParams(SceneCameraParamsInfo sceneCameraParams)
    {
        sceneCameraParams.setCreateTime(DateUtils.getNowDate());

        // 检查是否已有相同的摄像头配置存在
        SceneCameraParamsInfo dbSceneCameraParams = new SceneCameraParamsInfo();
        dbSceneCameraParams.setScenariosType(sceneCameraParams.getScenariosType());
        dbSceneCameraParams.setTaskType(sceneCameraParams.getTaskType());

        // 查询场景名称
        String sceneName = sysDictDataService.selectDictLabel("task_scenarios_type",
                sceneCameraParams.getScenariosType());
        if (sceneName == null) {
            throw new BaseException("未查询到相应的场景名称");
        }
        sceneCameraParams.setScenariosName(sceneName);

        // 根据任务类型查询摄像头信息
        if (StringUtils.equals(sceneCameraParams.getTaskType(), "1")) {
            // 查询数据库
            CameraInfo cameraInfo = new CameraInfo();
            cameraInfo.setCameraId(sceneCameraParams.getCameraId());
            cameraInfo.setDeptCode(sceneCameraParams.getBankCode());
            cameraInfo.setStatus("0");
            cameraInfo = cameraInfoMapper.selectCameraInfo(cameraInfo);
            if (cameraInfo == null) {
                throw new BaseException("未查询到相应的摄像头数据");
            }
            sceneCameraParams.setCameraName(cameraInfo.getCameraName());

            dbSceneCameraParams.setCameraId(sceneCameraParams.getCameraId());
        } else {
            EquipmentConfig equipmentConfig = equipmentConfigMapper.selectEquipmentConfigByEquipmentId(sceneCameraParams.getEquipmentId());
            if (equipmentConfig == null || !StringUtils.equals("0", equipmentConfig.getStatus())) {
                throw new BaseException("未查询到相应的录像机数据");
            }
            sceneCameraParams.setEquipmentIp(equipmentConfig.getEquipmentIp());

            // 查询录像机通道信息
            ChannelConfig channelConfig = new ChannelConfig();
            channelConfig.setEquipmentChannel(sceneCameraParams.getEquipmentChannel());
            channelConfig.setEquipmentId(equipmentConfig.getEquipmentId());
            channelConfig.setStatus("0");

            channelConfig = channelConfigMapper.selectChannelConfig(channelConfig);
            if (channelConfig == null) {
                throw new BaseException("未查询到相应的录像机通道数据");
            }
            sceneCameraParams.setCameraName(channelConfig.getRemark());

            dbSceneCameraParams.setEquipmentId(sceneCameraParams.getEquipmentId());
            dbSceneCameraParams.setEquipmentChannel(sceneCameraParams.getEquipmentChannel());
        }

        List<SceneCameraParamsInfo> result = sceneCameraParamsMapper.selectSceneCameraParamsList(dbSceneCameraParams);
        if (result != null && !result.isEmpty()) {
            throw new BaseException("已存在相同的摄像头配置");
        }

        // 查询机构信息
        SysDept sysDept = deptService.selectDeptByCode(sceneCameraParams.getBankCode());
        if (sysDept == null || StringUtils.equals("1", sysDept.getStatus())) {
            throw new BaseException("机构信息不存在");
        }
        sceneCameraParams.setSubBankCode(sysDept.getParentCode());
        sceneCameraParams.setSubBankName(sysDept.getParentName());
        sceneCameraParams.setBankName(sysDept.getDeptName());

        return sceneCameraParamsMapper.insertSceneCameraParams(sceneCameraParams);
    }

    /**
     * 修改场景摄像头参数管理
     *
     * @param sceneCameraParams 场景摄像头参数管理
     * @return 结果
     */
    @Override
    public int updateSceneCameraParams(SceneCameraParamsInfo sceneCameraParams)
    {
        return sceneCameraParamsMapper.updateSceneCameraParams(sceneCameraParams);
    }

    /**
     * 批量删除场景摄像头参数管理
     *
     * @param ids 需要删除的场景摄像头参数管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneCameraParamsByIds(Long[] ids)
    {
        return sceneCameraParamsMapper.deleteSceneCameraParamsByIds(ids);
    }

    /**
     * 删除场景摄像头参数管理信息
     *
     * @param id 场景摄像头参数管理主键
     * @return 结果
     */
    @Override
    public int deleteSceneCameraParamsById(Long id)
    {
        return sceneCameraParamsMapper.deleteSceneCameraParamsById(id);
    }

    @Override
    public Map<String, String> querySceneParamsConfig(SceneCameraParamsInfo sceneCameraParams) {
        try {
            log.info("querySceneParamsConfig params:{}", JSON.toJSONString(sceneCameraParams));
            // 查询数据库配置
            SceneParamsConfig sceneParamsConfig = sceneCameraParamsMapper.querySceneParamsConfig(sceneCameraParams);
            log.info("querySceneParamsConfig result:{}", JSON.toJSONString(sceneParamsConfig));
            if (sceneParamsConfig != null) {
                Map<String, String> configMap = BeanUtils.convertObjectToMap(sceneParamsConfig);
                log.info("querySceneParamsConfig map:{}", JSON.toJSONString(configMap));
                return configMap;
            }
        } catch (Exception e) {
            log.error("querySceneParamsConfig error ", e);
        }
        return null;
    }
}
